<?php
class Admin_UserController extends Custom_Controller_Action
{
   
    public function init()
    {
         /* Initialize action controller here */
    }
    
    public function indexAction()
    {
        $user = new User_Model_DbTable_User();
        $page = $this->getRequest()->getParam('page');
        $searchkey = $this->getRequest()->getParam('searchkey');
        $user->setPaginator($this->Config->params->recordsPerPage, $page);
        $this->view->users = $user->getUserWithRoles($searchkey);
        $this->view->paginator = $user->getPaginator();
        $this->view->searchkey = $searchkey;
        $this->view->page = $page;
        
    }
    public function deleteAction()
    {
    	$userid = $this->getRequest()->getParam('id');
    	$page = $this->getRequest()->getParam('page');
    	$usermem  = new User_Model_DbTable_UserMembership();
        
    	$res = $usermem->changetodelete($userid);
    	if($res)
    	{
    		$this->addMessage('User is deleted successfully');
    	}
    	else
    	{
    		$this->addMessage('User is not deleted');
    	}
    	$this->RedirectUrl('/admin/user');
    }
    
    public function createAction()
    {
    	$form = new Admin_Form_CreateUser();
    	$user = new User_Model_DbTable_User();
    	$this->view->form = $form;
    	if($this->getRequest()->isPost())
    	{
    		$formdata = $this->getRequest()->getPost();
    		if($form->isValid($formdata))
    		{
    			$data = $form->getValidValues($formdata);
                        
                        // insert in user membership table
                        $objmember = new User_Model_DbTable_UserMembership();
                        $arraymem['payment_plan_id'] = $formdata['payment_plan_id'];
                        $arraymem['status'] = $formdata['status'];
                          // GET expire on
                         $trialdays = $this->Config->params->initialtrialdays*24*60*60;
                         $current = date('Y-m-d H:i:s');
                         $arraymem['switched_on'] = $current; 
                         $arraymem['expire_on'] = $formdata['expire_on'];
                         // get no of campaign
                         $objpaymentplan = new User_Model_DbTable_PaymentPlan();
                         $plan = $objpaymentplan->get($formdata['payment_plan_id']);
                         $arraymem['no_campaign'] = $plan[0]['no_campaign'];
                         
                         unset($data['payment_plan_id']);
                         unset($data['status']);
                         unset($data['expire_on']);
                        
                         $data['role_id']  = 2;
                         $data['password'] = md5($data['password']);
                        $res = $user->insert($data);
                        $arraymem['user_id'] = $res;
                        $objmember->insert($arraymem);
                        
                        
    			if($res)
    			{
    				$this->addMessage('User is created successfully');
    			}
    			else 
    			{
    				$this->addMessage('User is not created successfully','error');
    			}
    			$this->RedirectUrl('/admin/user');
    		}
    	}
    	
    }
    public function editAction()
    {
    	$userid = $this->getRequest()->getParam('id');
    	$user = new User_Model_DbTable_User();
    	$userinfo = $user->getOne($userid);
    	$form = new Admin_Form_CreateUser($userid);
    	$this->view->form = $form;
        ;
        
        $objmember = new User_Model_DbTable_UserMembership();
        $memberdetail = $objmember->getMemberDetail($userid);
        
        $userinfo['status'] = $memberdetail[0]['status'];
        $userinfo['payment_plan_id'] = $memberdetail[0]['payment_plan_id'];
        $userinfo['expire_on'] = $memberdetail[0]['expire_on'];
        
    	$form->populate($userinfo);
        $attribs['disabled'] = true;
      	if($this->getRequest()->isPost())
    	{
    		$formdata = $this->getRequest()->getPost();
    		if($form->isValid($formdata))
    		{
    			$data = $form->getValidValues($formdata);
                        unset($data['status']);
                        unset($data['payment_plan_id']);
                        unset($data['expire_on']);
                        //updating the users table
          		$where = 'id='.$userid;
    			if($data['password'] == '')
    			{
    				unset($data['password']);
    			}
                  	$res = $user->update($data, $where);
                        
                        //updating the user membership table
                        $arraymem['payment_plan_id'] = $formdata['payment_plan_id'];
                        $arraymem['status'] = $formdata['status'];
                        $arraymem['expire_on'] = $formdata['expire_on'];
                        unset($where);
                        $where = "user_id=".$userid." AND record_type='CURRENT'";
                        $res1 = $objmember->update($arraymem, $where);
                        if($res || $res1)
    			{
    				$this->addMessage('User is updated successfully');
    			}
    			else 
    			{
    				$this->addMessage('User is not updated','error');
    			}
                     $this->RedirectUrl('/admin/user');   
    		}
                
    	}
    	
    }
}


?>
